<?php
namespace jy_weishop_en;
defined('BY_JYA') or exit('error');
/*
	营销卡
*/
class libao extends \plugin{
	public function add_libao($params){
		$sale_card=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'active_id'=>$params['active_id'],
			'order_id'=>$params['order_id'],
			'status'=>0,
			'title'=>$params['title'],
			'logo'=>$params['logo'],
			'price'=>$params['price'],
			'goods_id'=>intval($params['goods_id']),
			'coupon_id'=>intval($params['coupon_id']),
			'member_levelid'=>intval($params['member_levelid']),
			'zhe_fee'=>$params['zhe_fee'],
			'del_fee'=>$params['del_fee'],
			'del_goods_ids'=>$params['del_goods_ids'],
			'zhe_goods_ids'=>$params['zhe_goods_ids'],
			'type'=>intval($params['type']),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_sale_libao',$sale_card);
		$id=pdo_insertid();
		$news=array(
			'title'=>'恭喜，您获得《'.$params['title'].'》',
			'description'=>"快来查看吧，礼品多多哦，请及时使用哦！",
			'url'=>custom_url('jy_weishop_en/sale.libao/detail/id='.$id,true),
			'picurl'=>$params['logo'],
		);
		model('core/account')->send_news($news,$params['mid']);
		return true;
	}
	//使用
	public function open_libao($id){
		$libao=pdo_get('jy_weishop_sale_libao',array('id'=>$id,'mid'=>$_SESSION['uid'],'status'=>0));
		if(!$libao || !$libao['id']){
			exi('该礼包不存在！！','error','jy_weishop_en/sale.libao/');
		}
		if($libao['type']==0){//优惠券
			model('jy_weishop_en/coupon')->getcoupon($libao['coupon_id'],$libao['mid'],true);
		}elseif($libao['type']==1){//减免权益
			exi('前往购买商品!','error','jy_weishop_en/activity//order=sale_libao&sale_libaoid='.$id);				
		}elseif($libao['type']==2){//折扣权益
			exi('前往购买商品!','error','jy_weishop_en/activity//order=sale_libao&sale_libaoid='.$id);					
		}elseif($libao['type']==3){//赠送商品
			$goods=pdo_get('jy_weishop_goods',array('id'=>$libao['goods_id']),array('id','image','title','price','type','isverify','goodssn','productsn'));
			$libao['sendtype']=pdo_getcolumn('jy_weishop_order',array('id'=>$libao['order_id']),'sendtype');
			//已经整理出了商品类型type,isverify，只要支持核销，无论什么商品都可以核销！
			if($vv['isverify']==1 && $vv['type']!=2){
					if($vv['type']==3){
						$this->model('order')->order_send_verifygoods($libao['order_id'],$vv['goodsid'],$libao['sendtype']);
					}elseif($vv['type']==0){
						//实体物品，需配送时，依然生成核销码，但是不刷新配送状态
						$this->model('order')->order_send_verify($libao['order_id'],$vv['goodsid'],$libao['sendtype']);
					}else{
						$this->model('order')->order_send_verify($libao['order_id'],$vv['goodsid'],$libao['sendtype']);
					}
			}elseif($vv['type']==1 || $vv['type']==2){
					//虚拟发货，如果是自动发货
					$goods=pdo_get('jy_weishop_goods',array('id'=>$vv['goodsid']),array('type','virtualsend','virtualsendcont','virtual_template_id'));
					if(($goods['virtualsend']) || $vv['type']==2){
						$vv=array_merge($vv,$goods);
						$this->model('order')->order_send_virtual($libao['order_id'],$vv);
					}
			}
			$gifts[]=$goods;
			$new_gift=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$libao['mid'],
					'order_id'=>$libao['order_id'],
					'gift_id'=>0,
					'createtime'=>TIMESTAMP,
					'gifts'=>ijson_encode($gifts),
			);
			pdo_insert('jy_weishop_order_gift',$new_gift);
		}elseif($libao['type']==4){//会员礼包
			$next_level=pdo_getcolumn('jy_weishop_member_level',array('id'=>$libao['member_levelid']),'level');
			$current_level=pdo_getcolumn('jy_weishop_member',array('mid'=>$libao['mid']),'level');
			if($current_level<$next_level){
				$this->model('member')->up_member_level($libao['mid'],$libao['member_levelid']);
			}else{
				exi('您当前等级已经高于待升级等级，无需升级！','error');
			}		
		}
		pdo_update('jy_weishop_sale_libao',array('status'=>1),array('id'=>$libao['id']));
		return true;
	}
	//使用
	public function getmylibao($id){
		
	}
	//核对数量
	public function checkbuy($id){
		
	}
	//使用
	public function get_active($goods){
		$libaos=pdo_getall('jy_weishop_sale_libao',array('type'=>1,'status'=>0,'mid'=>$_SESSION['uid']));
		foreach($libaos as $lv){
			$del_goods=$lv['del_goods_ids']?json_decode($lv['del_goods_ids'],true):array();
			if(is_array($del_goods)){
				foreach($del_goods as $gv){
					if($gv['id']==$goods['id']){
						$active=array(
							'libao_id'=>$lv['id'],
							'nocart'=>true,
							'title'=>'减免'.$lv['del_fee'],
							'price'=>$goods['price']-$lv['del_fee'],
							'del_price'=>$lv['del_fee'],
							'del_fee'=>$lv['del_fee'],
						);
						$active['price']=$active['price']>0?$active['price']:0.00;
						return $active;
					}
				}
			}
			$del_goods=$lv['zhe_goods_ids']?json_decode($lv['zhe_goods_ids'],true):array();
			if(is_array($del_goods)){
			foreach($del_goods as $gv){
				if($gv['id']==$goods['id']){
					$active=array(
						'libao_id'=>$lv['id'],
						'nocart'=>true,
						'title'=>'打'.$lv['zhe_fee'].'折',
						'price'=>$lv['zhe_fee']*$goods['price']/10,
						'del_fee'=>$lv['zhe_fee'],
						'del_price'=>$goods['price']-$lv['zhe_fee']*$goods['price']/10,
					);
					return $active;
				}
			}
			}
		}
		return false;
	}
	//选择
	public function choose_libao($id,$post=array()){
		$libaos=str_array($post['libaos']);
		$log=pdo_get('jy_giftbao_active_log',array('id'=>$id));
		$log['order_id']=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$log['ordersn']),'id');
		foreach($libaos as &$lv){
			$libao=pdo_get('jy_giftbao_libao',array('id'=>$lv));
			$lv=array_fields(array('id','logo','title','type','price'),$libao);
			$libao['mid']=$log['mid'];
			$libao['active_id']=$log['active_id'];
			$libao['order_id']=$log['order_id'];
			$this->add_libao($libao);
		}
		file_put_contents(ROOT_D.'/11.TXT',$id);
		pdo_update('jy_giftbao_active_log',array('status'=>1,'libaos'=>ijson_encode($libaos)),array('id'=>$id));
		return true;
	}
}